// 3. 无重复字符的最长子串
// https://leetcode.cn/problems/longest-substring-without-repeating-characters/

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
    let set = new Set();
    let maxLen = 0;
    let left = 0;
    for (let right = 0; right < s.length; right++) {
        if (!set.has(s[right])) {
            set.add(s[right]);
            maxLen = Math.max(maxLen, right - left + 1);
        } else {
            // 移动左指针，清除重复元素
            // 注意，删除掉第一个重复的元素即可。因为滑动窗口的右指针每次只移动一格，所以每次仅多出一个重复元素

            while (left >= 0) {
                const val = s[left];
                left++;
                if (val === s[right]) break;
            }

            set = new Set(s.substring(left, right + 1).split(''));
        }
    }

    console.log(`maxLen is ${maxLen}`);
    return maxLen;
};

lengthOfLongestSubstring('abcabcbb');
lengthOfLongestSubstring('bbbbb');
lengthOfLongestSubstring('pwwkew');
